/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.cjb.malacma.rssfeed.util;

import com.google.gdata.client.GoogleService;
import com.google.gdata.util.AuthenticationException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.cjb.malacma.rssfeed.entity.UserContext;
import static net.cjb.malacma.rssfeed.persistence.JPAController.*;

/**
 *
 * @author malacma
 */
public final class ContextFacade {

    public static final String PASS = "pass";
    public static final String USER = "user";

    public static final void authenticate(PrintWriter out, HttpServletRequest request, HttpServletResponse response) throws Exception {

        HttpSession session = request.getSession(true);
        LocalContext lc = new LocalContext(session);
        boolean hasError = false;
        if (!lc.isSet()) {
            String ppass, puserName;

            ppass = request.getParameter(PASS);
            puserName = request.getParameter(USER);

            UserContext user = lc.getUserInstance(USER);
            user.setPass(ppass);
            user.setEmail(puserName);

            GoogleService gc = new GoogleService("cl", java.util.UUID.randomUUID().toString());
            try {
                gc.setUserCredentials(puserName, ppass);
                user.setMyService(gc);

                lc.setCurrent(USER, user);
            } catch (AuthenticationException authenticationException) {
                out.print(authenticationException.toString());
                out.print("<a href='javascript:history.back()'><<<<<<<</a>");
                hasError = true;
            }

        }
        if (!hasError) {
            out.print("<script>");
            out.print("parent.nefertiteController.hideLoading();");
            out.print("parent.nefertiteController.closeWindow();");
            out.print("parent.nefertiteController.reloadMainWin();");
            out.print("</script>");
        }
    }

    public static UserContext getCurrentUse(HttpServletRequest request){

        LocalContext lc = new LocalContext(request.getSession(true));

        return lc.getUserInstance(USER);

    }
}

class LocalContext {

    LocalContext(HttpSession s) {
        this.localSession = s;
    }
    private HttpSession localSession;

    UserContext getUserInstance(String string) {
        UserContext u = (localSession.getAttribute(string) == null ? new UserContext() : (UserContext) localSession.getAttribute(string));
        return u;

    }

    boolean isSet() {
        return this.localSession.getAttribute(ContextFacade.USER) == null ? false : true;
    }

    void setCurrent(String string, UserContext user) throws Exception {
        UserContext u1 = null;

        try {
            u1 = findUserContextByMail(user.getEmail());
            u1.setPass(user.getPass());
        } catch (Exception e) {
            u1 = user;
            persist(u1);
        }

        localSession.setAttribute(string, u1);
    }
}
